package com.foreknow.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Department;
import com.foreknow.entity.User;
import com.foreknow.service.DepartmentService;
import com.foreknow.utils.Constance;
import com.foreknow.utils.TokenUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @RequestMapping(value = "/findDepartment", method = RequestMethod.GET)
    public List<Department> findUserByPage() throws JsonProcessingException {
        return  departmentService.queryDept("");
    }

    @RequestMapping(value = "findDepartmentByPage",method = RequestMethod.GET)
    public String findDeptByPage(@RequestParam Map<String,String> para) throws JsonProcessingException {
        //获取参数
        String query = para.get("query");
        Integer pageNum = Integer.parseInt(para.get("pageNum"));
        Integer pageSize = Integer.parseInt(para.get("pageSize"));
        HashMap<String, Object> hs = new HashMap<>();
        //设置分页信息
        PageHelper.startPage(pageNum, pageSize);
        List<Department> deptList = departmentService.queryDept(query);
        PageInfo<Department> info = new PageInfo<>(deptList);
        //给map添加数据
        hs.put("totalpage", info.getTotal());
        hs.put("pagenum", info.getPageNum());
        hs.put("depts", deptList);
        if (deptList.size() > 0) {//如果获取到数据
            hs.put("meta", new Constance("获取成功", 200));
        } else {
            hs.put("meta", new Constance("没有查询到数据", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "searchDept",method = RequestMethod.POST)
    public String searchDept(@RequestBody Map<String,Object> para) throws JsonProcessingException {
        //获取参数
        ObjectMapper objectMapper = new ObjectMapper();
        Object heightQuery = para.get("heightQuery");
        Department department = objectMapper.convertValue(heightQuery,Department.class);
        Integer pageNum = Integer.parseInt(String.valueOf(para.get("pageNum")));
        Integer pageSize = Integer.parseInt(String.valueOf(para.get("pageSize")));
        HashMap<String, Object> hs = new HashMap<>();
        //设置分页信息
        PageHelper.startPage(pageNum, pageSize);
        List<Department> deptList = departmentService.heightQueryDept(department);
        PageInfo<Department> info = new PageInfo<>(deptList);
        //给map添加数据
        hs.put("totalpage", info.getTotal());
        hs.put("pagenum", info.getPageNum());
        hs.put("depts", deptList);
        if (deptList.size() > 0) {//如果获取到数据
            hs.put("meta", new Constance("获取到"+info.getTotal()+"条数据", 200));
        } else {
            hs.put("meta", new Constance("没有查询到数据", 401));
        }

        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "deleteDeptById",method = RequestMethod.DELETE)
    public String deleteDept(@RequestParam Integer id) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        if(departmentService.removeDeptById(id)){
            hs.put("meta", new Constance("删除成功", 200));
        }else{
            hs.put("meta", new Constance("删除失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/deleteDeptsById", method = RequestMethod.POST)
    public String deleteUsers(@RequestBody Map<String,List<Integer>>ids) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        Integer num = departmentService.removeDeptsById(ids.get("ids"));
        if(num>0){
            hs.put("meta", new Constance("删除成功！"+num+"条数据被删除", 200));
        }else{
            hs.put("meta", new Constance("删除失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
    @RequestMapping(value = "/addDept",method = RequestMethod.POST)
    public String addUser(@RequestBody Department department)throws JsonProcessingException{
        HashMap<String, Object> hs = new HashMap<>();
        if(departmentService.addDept(department)){
            hs.put("meta", new Constance("添加成功", 200));
        }else{
            hs.put("meta", new Constance("添加失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value="/findDeptById",method = RequestMethod.GET)
    public String findUserById(@RequestParam Integer id) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        Department department = departmentService.queryDeptById(id);
        if(department!=null){
            hs.put("meta", new Constance("添加成功", 200));
        }else{
            hs.put("meta", new Constance("添加失败", 401));
        }
        hs.put("data", department);
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/modifyDept", method = RequestMethod.PUT)
    public String modifyUser(@RequestBody Department department) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        if(departmentService.modifyDept(department)){
            hs.put("meta", new Constance("修改成功", 200));
        }else{
            hs.put("meta", new Constance("修改失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }



}
